package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.dto.SortingResultDto;
import me.zhengjie.pojo.entity.WcsFluentFrameConfig;
import me.zhengjie.pojo.entity.WcsFluentFrameOutboundDetails;
import me.zhengjie.pojo.entity.WcsFluentFrameOutboundDetailsHistory;
import me.zhengjie.pojo.vo.ReturnVo;
import me.zhengjie.service.WmsService;
import me.zhengjie.sqlservice.WcsFluentFrameConfigService;
import me.zhengjie.sqlservice.WcsFluentFrameOutboundDetailsHistoryService;
import me.zhengjie.sqlservice.WcsFluentFrameOutboundDetailsService;
import me.zhengjie.tcp.FluentFrameTCPA;
import me.zhengjie.tcp.FluentFrameTCPB;
import me.zhengjie.util.state.MaterialStatus;
import me.zhengjie.util.state.TaskType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class WmsServiceImpl implements WmsService {
    private static final Logger wms = LoggerFactory.getLogger("wms");

    private final WcsFluentFrameConfigService wcsFluentFrameConfigService;

    private final FluentFrameTCPA fluentFrameTCPA;

    private final FluentFrameTCPB fluentFrameTCPB;

    private final WcsFluentFrameOutboundDetailsService wcsFluentFrameOutboundDetailsService;

    private final WcsFluentFrameOutboundDetailsHistoryService wcsFluentFrameOutboundDetailsHistoryService;
    @Override
    public ReturnVo sortingResult(SortingResultDto sortingResult) {
        wms.info("2楼分拣完成后上报WCS请求参数：{}",sortingResult);
        Integer type = sortingResult.getType();
        String containerCode = sortingResult.getContainerCode();
        WcsFluentFrameOutboundDetails wcsFluentFrameOutboundDetails = wcsFluentFrameOutboundDetailsService.selectContainerCode(containerCode);
        if (wcsFluentFrameOutboundDetails !=null){
            //根据储位编码查流利架
            String storageCode = wcsFluentFrameOutboundDetails.getStorageCode();
            Integer idDetails = wcsFluentFrameOutboundDetails.getId();
            WcsFluentFrameConfig wcsFluentFrameConfig = wcsFluentFrameConfigService.selectStorageCode(storageCode);
            Integer id = wcsFluentFrameConfig.getId();
            String region = wcsFluentFrameConfig.getRegion();
            Integer unitId = wcsFluentFrameConfig.getUnit_id();
            Integer cylinderControlAddress = wcsFluentFrameConfig.getCylinderControlAddress();
            Integer size = wcsFluentFrameConfig.getSize();
            size--;
            //修改数量
            boolean b2 = wcsFluentFrameConfigService.updateSize(id, size);
            //整箱出库，清空流利架上面的容器
//            boolean b = wcsFluentFrameConfigService.wcsFluentFrameOutboundDetailsService(id, null);
            if (type == 1){
                WcsFluentFrameOutboundDetailsHistory wcsFluentFrameOutboundDetailsHistory = new WcsFluentFrameOutboundDetailsHistory();
                BeanUtils.copyProperties(wcsFluentFrameOutboundDetails,wcsFluentFrameOutboundDetailsHistory);
                wcsFluentFrameOutboundDetailsHistory.setId(null);
                wcsFluentFrameOutboundDetailsHistoryService.insertFluentFrameOutboundDetailsHistory(wcsFluentFrameOutboundDetailsHistory);
                //整箱出库删除流利架出库明细
                wcsFluentFrameOutboundDetailsService.deleteId(idDetails);
                //往流利架地址位写入
                if (TaskType.FLUENT_FRAME_A.getMessage().equals(region)){
                    boolean b1 = fluentFrameTCPA.writeWithRetry(cylinderControlAddress, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                }else if (TaskType.FLUENT_FRAME_B.getMessage().equals(region)){
                    boolean b1 = fluentFrameTCPB.writeWithRetry(cylinderControlAddress, 1, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                }
            }else if (type == 2){
                //分拣出库 修改状态为分拣出库
                wcsFluentFrameOutboundDetailsService.updateIsNest(idDetails,1);
                //往流利架地址位写入
                if (TaskType.FLUENT_FRAME_A.getMessage().equals(region)){
                    boolean b1 = fluentFrameTCPA.writeWithRetry(cylinderControlAddress, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                }else if (TaskType.FLUENT_FRAME_B.getMessage().equals(region)){
                    boolean b1 = fluentFrameTCPB.writeWithRetry(cylinderControlAddress, 2, unitId, MaterialStatus.RETRY_COUNT.getStatus());
                }
            }
        }
        return ReturnVo.ng("容器编码不存在");
    }
}
